using System;
using System.Windows.Forms;
using System.ComponentModel.Composition;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.Xml;

namespace SS.Ynote.Classic
{
    public partial class Preferences : Form
    {
        private Dictionary<FastColoredTextBoxNS.Language,string[]> LanguageExtensions;
        private IYnoteHost Host { get; set; }
        /// <summary>
        /// Default Constructor
        /// </summary>
        public Preferences(IYnoteHost host)
        {
            InitializeComponent();
            LanguageExtensions = new Dictionary<FastColoredTextBoxNS.Language, string[]>();
            foreach (System.Text.EncodingInfo info in System.Text.Encoding.GetEncodings())
                listBox3.Items.Add(info.DisplayName);
            foreach (var wwm in Enum.GetValues(typeof(FastColoredTextBoxNS.WordWrapMode)))
                cmbwordwrapmode.Items.Add(wwm);
            foreach (var type in Enum.GetValues(typeof(FastColoredTextBoxNS.HighlightingRangeType)))
                cmbhighlightstrategy.Items.Add(type);
            foreach (var type in Enum.GetValues(typeof(FastColoredTextBoxNS.TextAreaBorderType)))
                this.cmbtextareamode.Items.Add(type);
            this.label3.Text = "No. of Encodings Supported : " + listBox3.Items.Count;
            this.comboBox1.SelectedIndex = 0;
            LoadPluginList();
            foreach (var item in Enum.GetValues(typeof(FastColoredTextBoxNS.Language)))
                listBox1.Items.Add(item);
            Host = host;
            this.tablocation.Text = YnoteSettingsBase.TabLocation.ToString();
            this.cmbwordwrapmode.Text = YnoteSettingsBase.WordWrapMode.ToString();
            this.cmbhighlightstrategy.Text = YnoteSettingsBase.HighlightStrategy.ToString();
            this.cmbuitheme.Text = Properties.Settings.Default.UITheme.ToString();
            this.cmbtextareamode.Text = YnoteSettingsBase.TextAreaType.ToString();
            this.ShowRuler.Checked = YnoteSettingsBase.AlwaysShowRuler;
            this.ShowLineNumber.Checked = YnoteSettingsBase.ShowLineNumbers;
            this.showcaret.Checked = YnoteSettingsBase.ShowCaret;
            this.showfoldinglines.Checked = YnoteSettingsBase.ShowFoldingLines;
            this.txtlineinterval.Text = YnoteSettingsBase.LineInterval.ToString();
            this.txtpaddingwidth.Text = YnoteSettingsBase.PaddingWidth.ToString();
            BuildDictionary();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://ynoteplugins.codeplex.com");
        }

        private void cmbhighlightstrategy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbhighlightstrategy.SelectedIndex == 0)
                this.label12.Text = "Highlight Only Changed Range of Text.\r\nHigh Performance.";
            else if (cmbhighlightstrategy.SelectedIndex == 1)
                this.label12.Text = "Highlight Visible Range of Text.\r\nMedium Performance.";
            else if (cmbhighlightstrategy.SelectedIndex == 2)
                this.label12.Text = "Highlight All(Visible and Invisible) Text.\r\nLow Performance.";
        }
        void BuildDictionary()
        {
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.Text, new string[]{".txt"});
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.HTML, YnoteSettingsBase.HTMLExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.ASP, YnoteSettingsBase.ASPExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.Actionscript, YnoteSettingsBase.ActionscriptExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.Assemblyx86, YnoteSettingsBase.AssemblyExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.Batch, YnoteSettingsBase.BatchExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.C, YnoteSettingsBase.CExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.CPP, YnoteSettingsBase.CPPExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.CSharp, YnoteSettingsBase.CSExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.D, YnoteSettingsBase.DExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.CSS, YnoteSettingsBase.CSSExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.FSharp, YnoteSettingsBase.FSharpExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.INI, YnoteSettingsBase.INIExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.Java, YnoteSettingsBase.JavaExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.Javascript, YnoteSettingsBase.JScriptExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.JSON, YnoteSettingsBase.JSONExtension.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.Lisp, YnoteSettingsBase.LispExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.Lua, YnoteSettingsBase.LuaExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.OpenCL, YnoteSettingsBase.OpenCLExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.Pascal, YnoteSettingsBase.PascalExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.Perl, YnoteSettingsBase.PerlExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.PHP, YnoteSettingsBase.PHPExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.Python, YnoteSettingsBase.PythonExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.Ruby, YnoteSettingsBase.RubyExtension.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.Scala, YnoteSettingsBase.ScalaExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.SQL, YnoteSettingsBase.SQLExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.VB, YnoteSettingsBase.VBExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.QBasic, YnoteSettingsBase.QBasicExtensions.ToArray());
            LanguageExtensions.Add(FastColoredTextBoxNS.Language.Xml, YnoteSettingsBase.XMLExtensions.ToArray());
        }
        void LoadPluginList()
        {
            LoadList();
            treeView1.Nodes.Clear();
            TreeNode t = new TreeNode();
            TreeNode tdrop1 = new TreeNode();
            TreeNode tdrop2 = new TreeNode();
            t.Text = "Plugins";
            tdrop1.Text = "General Plugins";
            tdrop2.Text = "ContextMenu Plugins";
            foreach (var plugin in GeneralPlugins)
                tdrop1.Nodes.Add(plugin.Name);
            foreach (var contextmenuplugin in ContextMenuPlugins)
                tdrop2.Nodes.Add(contextmenuplugin.Name);
            t.Nodes.AddRange(new TreeNode[] { tdrop1, tdrop2 });
            treeView1.Nodes.Add(t);
        }
        [ImportMany("IYnotePlugin")]
        private IEnumerable<IYnotePlugin> GeneralPlugins { get; set; }
        [ImportMany("IContextMenuPlugin")]
        private IEnumerable<IContextMenuPlugin> ContextMenuPlugins { get; set; }
        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            LoadPluginList();
        }
        void LoadList()
        {
            var dircatalog = new DirectoryCatalog(Application.StartupPath + "\\Plugins");
            var container = new CompositionContainer(dircatalog);
            GeneralPlugins = container.GetExportedValues<IYnotePlugin>();
            ContextMenuPlugins = container.GetExportedValues<IContextMenuPlugin>();
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://ynoteplugins.codeplex.com");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            System.IO.Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Samarjeet_Singh"); ;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Reset();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            System.IO.File.WriteAllText(Application.StartupPath + "\\Recent.info", string.Empty);
        }

        private void cmbuitheme_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                SS.Ynote.Classic.Properties.Settings.Default.UITheme = (UITheme)cmbuitheme.SelectedItem;
            }
            catch (Exception)
            {
                return;
            }
        }

        private void linkLabel6_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Close();
            Host.OpenFile(YnoteSettingsBase.SettingsFile, 0);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox2.Items.Clear();
            string[] lst = new string[]{};
            FastColoredTextBoxNS.Language lang = (FastColoredTextBoxNS.Language)listBox1.SelectedItem;
            LanguageExtensions.TryGetValue(lang, out lst);
            listBox2.Items.AddRange(lst);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void linkLabel8_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Host.OpenFile(YnoteSettingsBase.SettingsFile, 0);
        }

        private void linkLabel7_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Host.OpenFile(YnoteSettingsBase.SettingsFile, 0);
        }
      
    }
}
